package com.weijianhuawen.wjblog.web.controller;

import com.weijianhuawen.wjblog.common.aspect.ApiOperationLog;
import com.weijianhuawen.wjblog.common.utils.JsonUtil;
import com.weijianhuawen.wjblog.common.utils.Response;
import com.weijianhuawen.wjblog.web.mode.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-15 1:18
 */

@RestController
@Slf4j
//添加api名称
@Api(tags = "测试模块")
public class TestController {

    @PostMapping("/admin/test")
    @ApiOperationLog(description = "测试")
    @ApiOperation(value = "测试模块-测试接口")
    public Object test(@RequestBody @Validated User user) {
        // 打印入参
        log.info(JsonUtil.toJsonString(user));

        // 设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateDate(LocalDate.now());
        user.setTime(LocalTime.now());
        return Response.success(user);
    }

    @PostMapping("admin/update")
    @ApiOperationLog(description = "鉴权测试")
    @ApiOperation(value = "测试模块-鉴权测试")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Object update() {
        log.info("update success!!!");
        return Response.success();
    }

//    @PostMapping("/test")
//    @ApiOperationLog(description = "测试")
//    public Object test(@RequestBody @Validated User user, BindingResult result) {
//        if (result.hasErrors()) {
//            //获取不通过原因
//            String errorMsg = result.getFieldErrors()
//                    .stream()
//                    .map(FieldError::getDefaultMessage)
//                    .collect(Collectors.joining(", "));
//            return Response.fail(errorMsg);
//        }
//        throw new UserException(Code.USER_ERROR);
////        return Response.success();
//    }
}
